<?php
require 'inc.php';
@header('Content-Type: text/html; charset=UTF-8');
?>
<!DOCTYPE html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>正在为您跳转到支付页面，请稍候...</title>
    <style type="text/css">
body{margin:0;padding:0}
p{position:absolute;left:50%;top:50%;height:35px;margin:-35px 0 0 -160px;padding:20px;font:bold 16px/30px "宋体",Arial;background:#f9fafc url(./assets/img/loading.gif) no-repeat 20px 20px;text-indent:40px;border:1px solid #c5d0dc}
#waiting{font-family:Arial}
    </style>
</head>
<body>
<?php

$type = isset($_GET['type']) ? $_GET['type'] : exit('No type!');
$order = isset($_GET['order']) ? $_GET['order'] : exit('No order!');
$row = $DB->getRow("SELECT * FROM pre_orders WHERE trade_no=:trade_no LIMIT 1", [':trade_no' => $order]);
if (!$row['trade_no']) {
    exit('该订单号不存在，请返回来源地重新发起请求！');
}

if ($row['money'] <= 0 || !preg_match('/^[0-9.]+$/', $row['money'])) {
    exit('订单金额不合法');
}

if ($row['status'] >= 1) {
    exit('该订单已支付完成，请<a href="/user?act=pay">返回重新生成订单</a>');
}

if ($type == 'alipay' && $conf['pay_alipay'] == 4 || $type == 'qqpay' && $conf['pay_qqpay'] == 3 || $type == 'wxpay' && $conf['pay_wxpay'] == 4) { //码支付
    $DB->exec("UPDATE `pre_orders` SET `type`=:type,channel=:channel WHERE `trade_no`=:trade_no", [':type' => $type, ':channel' => 'codepay', ':trade_no' => $order]);
    echo "<script>window.location.href='./codepay.php?type={$type}&trade_no={$order}';</script>";

} elseif ($type == 'alipay' && $conf['pay_alipay'] == 3 || $type == 'qqpay' && $conf['pay_qqpay'] == 2 || $type == 'wxpay' && $conf['pay_wxpay'] == 3) { //易支付
    $DB->exec("UPDATE `pre_orders` SET `type`=:type,channel=:channel WHERE `trade_no`=:trade_no", [':type' => $type, ':channel' => 'easypay', ':trade_no' => $order]);
    require_once SYSTEM_ROOT . "epay/epay.config.php";
    require_once SYSTEM_ROOT . "epay/epay_submit.class.php";
    $parameter = array(
        "pid" => trim($alipay_config['partner']),
        "type" => $type,
        "notify_url" => $siteurl . 'epay_notify.php',
        "return_url" => $siteurl . 'epay_return.php',
        "out_trade_no" => $order,
        "name" => $row['name'],
        "money" => $row['money'],
        "sitename" => $conf['web_name'],
    );
    //建立请求
    $alipaySubmit = new AlipaySubmit($alipay_config);
    $html_text = $alipaySubmit->buildRequestForm($parameter, "POST", "正在跳转");
    echo $html_text;
} elseif ($type == 'alipay' && $conf['pay_alipay'] == 1) { //当面付
    $DB->exec("UPDATE `pre_orders` SET `type`=:type,channel=:channel WHERE `trade_no`=:trade_no", [':type' => $type, ':channel' => 'alipay', ':trade_no' => $order]);
    echo "<script>window.location.href='./alipay.php?trade_no={$order}';</script>";

} elseif ($type == 'alipay' && $conf['pay_alipay'] == 2) { //支付宝
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
        include SYSTEM_ROOT . 'alipay/wxopen.php';
        exit;
    }
    $DB->exec("UPDATE `pre_orders` SET `type`=:type,channel=:channel WHERE `trade_no`=:trade_no", [':type' => $type, ':channel' => 'alipay', ':trade_no' => $order]);
    $ordername = !empty($conf['ordername']) ? str_replace('[time]', time(), $conf['ordername']) : $row['name'];

    if (checkmobile() == true) {
        require_once SYSTEM_ROOT . "alipay/model/builder/AlipayTradeWapPayContentBuilder.php";
        require_once SYSTEM_ROOT . "alipay/AlipayTradeService.php";

        //构造参数
        $payRequestBuilder = new AlipayTradeWapPayContentBuilder();
        $payRequestBuilder->setSubject($ordername);
        $payRequestBuilder->setTotalAmount($row['money']);
        $payRequestBuilder->setOutTradeNo($order);

        $aop = new AlipayTradeService($config);
        echo $aop->wapPay($payRequestBuilder);
    } else {
        require_once SYSTEM_ROOT . "alipay/model/builder/AlipayTradePagePayContentBuilder.php";
        require_once SYSTEM_ROOT . "alipay/AlipayTradeService.php";

        //构造参数
        $payRequestBuilder = new AlipayTradePagePayContentBuilder();
        $payRequestBuilder->setSubject($ordername);
        $payRequestBuilder->setTotalAmount($row['money']);
        $payRequestBuilder->setOutTradeNo($order);

        $aop = new AlipayTradeService($config);
        echo $aop->pagePay($payRequestBuilder);
    }
} elseif ($type == 'wxpay' && ($conf['pay_wxpay'] == 1 || $conf['pay_wxpay'] == 2)) { //微信
    $DB->exec("UPDATE `pre_orders` SET `type`=:type,channel=:channel WHERE `trade_no`=:trade_no", [':type' => $type, ':channel' => 'wxpay', ':trade_no' => $order]);
    if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) {
        echo "<script>window.location.href='./wxjspay.php?trade_no={$order}&d=1';</script>";
    } elseif (checkmobile() == true) {
        echo "<script>window.location.href='./wxwappay.php?trade_no={$order}';</script>";
    } else {
        echo "<script>window.location.href='./wxpay.php?trade_no={$order}';</script>";
    }
} elseif ($type == 'qqpay' && $conf['pay_qqpay'] == 1) { //QQ
    $DB->exec("UPDATE `pre_orders` SET `type`=:type,channel=:channel WHERE `trade_no`=:trade_no", [':type' => $type, ':channel' => 'qqpay', ':trade_no' => $order]);
    if (checkmobile() == true) {
        echo "<script>window.location.href='./qqwappay.php?trade_no={$order}';</script>";
    } else {
        echo "<script>window.location.href='./qqpay.php?trade_no={$order}';</script>";
    }
} else {
    exit('该支付方式已关闭');
}

?>
<p>正在为您跳转到支付页面，请稍候...</p>
</body>
</html>